package com.etc.team2.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.team2.bean.Line;
import com.etc.team2.service.LineService;

public class AddLineServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 从addline.jsp获取line信息，放到一个Line对象中
		 * 调用AddLineServlet的addLine方法添加路线
		 * 成功，跳转到AllLineServlet
		 * */
		request.setCharacterEncoding("utf-8");
		String referer=request.getHeader("referer");
		if (referer==null||!referer.startsWith("http://localhost:")) {
			response.sendRedirect("home.jsp");
			return;
		}
		String lineName=request.getParameter("lineName");
		String firsttime=request.getParameter("firsttime");
		String lasttime=request.getParameter("lasttime");
		double price=Double.parseDouble(request.getParameter("price"));
		String[] stationlist=request.getParameter("stationlist").split(",");
		List<String> stationlist2=new ArrayList<String>();
		for (String station :stationlist ) {
			stationlist2.add(station);
		}
		Line line=new Line(lineName, firsttime, lasttime, price, stationlist2);
		if(new LineService().addLine(line)){
			response.sendRedirect("AllLineServlet");
		}else {
			System.out.println("插入失败");
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}
